import sys, urllib, urllib2, re
from data.instances import instances
from data.abstracts import abstracts
from data.words import names
from random import choice, random, shuffle, randint

f = open('data/instance_types_en.nt', 'r', 1)
assert(f)

r1 = re.compile(r'<[^>]+>\s*<[^>]+>\s*<([^>]+)> \.')
def getClassForItem(itm):
   offset = instances[itm]
   f.seek(offset)
   line = f.readline().strip()
   m = r1.match(line)
   if m:
      class_ = m.group(1).split('/')[-1]
      if '#' in class_:
         class_ = class_.split('#')[-1]
   else:
      sys.stderr.write('WARNING: failed to get class from %s\n' % line)
      class_ = 'Thing'
   return class_

f2 = open('data/short_abstracts_en.nt', 'r', 1)
assert(f2)

r2 = re.compile(r'<[^>]+>\s*<[^>]+>\s*"(.+?)"@en \.')
def getAbstractForItem(itm):
   try:
      offset = abstracts[itm]
      f2.seek(offset)
      line = f2.readline().strip()
      m = r2.match(line)
      if m:
         txt = m.group(1)
      else:
         sys.stderr.write('WARNING: failed to get abstract from %s\n' % line)
         txt = '-'
   except KeyError:
      sys.stderr.write('WARNING: failed to get offset for %s\n' % itm)
      txt = '-'
   return txt

if __name__ == "__main__":
    server = 'http://0.0.0.0:8080/'
    testing = False
    full_shuffle = True # do local shuffles each pass through the query set

    if len(sys.argv)>1:
       N = int(sys.argv[1])
    else:
       N = 100

    # create query list
    query_set = []
    name_set = []
    for i in range(N):
       abstract = '-'
       # make sure the instance we pick actually has a corresponding abstract
       while abstract=='-':
          if len(query_set)<2:
             # random new instance
             if testing:
                print 'new',
             name = choice(instances.keys())
          else:
             # similar to a query already in the set
             if testing:
                print 'similar',
             # fist pick a recenet query
             other_names = []
             while not other_names:
                if full_shuffle:
                   name = choice(name_set)
                else:
                   name = choice(name_set[-4:])
                # get the words that make up its name
                words = name.encode('ascii','replace').lower().replace(',','').split('_')
                # pick a different random instance name that shares at least 
                # one word
                word = choice(words)
                other_names = names[word][:]
                other_names.remove(name)
                other_names = [x for x in other_names if x not in name_set]
             name = choice(other_names)
          abstract = getAbstractForItem(name)
       values = {'QueryClass' : getClassForItem(name),
                 'QueryString' : name}
       query_set.append(values)
       name_set.append(name)
    # now actually make the queries
    for i in range(4):
       # random order each time through
       if full_shuffle:
          shuffle(query_set)
       else:
          m = randint(2,4)
          for j in range(0,len(query_set)-m,m):
             tmp = query_set[j]
             query_set[j] = query_set[j+m]
             query_set[j+m] = tmp
       for values in query_set:
          if testing:
             print values['QueryString']
          else:
             data = urllib.urlencode(values)
             req = urllib2.Request(server, data)
             response = urllib2.urlopen(req)
             the_page = response.read()
             if testing:
                print the_page
    # tell server to quit
    try:
       urllib2.urlopen(server+'stop')
    except:
       pass
